
@extends('layout.default')

@section('layout.content') 
<div class="row">
  <div class="col-xs-12 col-md-8 col-md-offset-2">
    <form class="form-horizontal" id="role-create" autocomplete="off">
    {{ csrf_field() }}
    <div class="form-group">
        <label class="col-sm-2 control-label no-padding-right" for="name"> <b><span class="red">*</span> 角色名称：</b> </label>
        <div class="col-sm-4">
            <input type="text" class="form-control" id="name" name="name" placeholder="角色名称" />
        </div>        
    </div>
    <div class="form-group">
        <label class="col-sm-2 control-label no-padding-right" for="name">  权限：</label>
        <div class="col-sm-10">          
          @foreach($permissions as $key=>$main)
          <div class="checkbox">
            <label class="no-padding-left">               
              <span class="lbl" ><b> {{$main->title}}</b></span>
            </label>
            @foreach ($main['children'] as $key=>$permission)
              <label>
                <input name="permission" type="checkbox" value="{{$permission->id}}" class="ace ace-checkbox-2" />
                <span class="lbl no-padding-left"> {{$permission->title}}</span>
              </label>
            @endforeach
          </div>
          @endforeach          
        </div>        
    </div>
    
    
    <div class="form-group">
      <label class="col-sm-2 control-label no-padding-right"> </label>
      <div class="col-sm-4">
          <button type="button" class="btn btn-primary submit">
            <i class="ace-icon fa fa-check"></i>
            <span class="">确定</span>
          </button>
      </div>
    </div>
    
  </form>
  </div>
</div>

@stop

@section('javascript')
<script type="text/javascript">
  $(function() {

    $('.submit').on('click', function() {
      var permissions = new Array(); 
      $("input[name='permission']:checked").each(function(){  
          permissions.push($(this).val());
      });
      //console.log(permissions);
      var uri = '{{route('system.role.store')}}';
      
      $.post(uri, {
        '_token':$("input[name='_token']").attr('value'),
        'name': $("input[name='name']").val(),
        'permissions': permissions,
        'is_submit': true
      }, function(data) {
        console.log(data);
        layer.msg(data.message, { shift: -1 }, function(){
          if(data.status === true && data.url != null ){
            $(window).attr('location', data.url);
          } 
        });

      },'json').error(function(data){
        layer.msg(data.responseJSON.message);
      }); 
      
    });
   

  });
</script>
@stop